//
//  LGHomeOrganView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/9/6.
//

#import "LGHomeOrganView.h"

@interface LGHomeOrganView()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , assign)CGFloat tableHeight;

@end

@implementation LGHomeOrganView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        [self addSubview:self.tableView];
        [self addSubview:self.closeBtn];
        [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tableView.mas_bottom);
            make.left.bottom.right.equalTo(self);
        }];
    }
    return self;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    CGFloat tableHeight = viewPix(62)*dataArray.count+viewPix(20);
    self.tableHeight = tableHeight>viewPix(400)?viewPix(400):tableHeight;
    [self.tableView reloadData];
}

-(void)showOrganViewAnimation{
    self.hidden = NO;
    [UIView animateWithDuration:0.4 animations:^{
        self.tableView.frame = CGRectMake(0, 0, Screen_W, self.tableHeight);
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.4];
    }];
}

-(void)hiddeOrganViewAnimation{
    [UIView animateWithDuration:0.4 animations:^{
        self.tableView.frame = CGRectMake(0, 0, Screen_W, 0);
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGHomeOrganCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    if (self.dataArray.count == 1) {
        cell.type = LGPositionTypeSingle;
    }else if (indexPath.row == 0){
        cell.type = LGPositionTypeTop;
    }else if (indexPath.row == self.dataArray.count-1){
        cell.type = LGPositionTypeBottom;
    }else{
        cell.type = LGPositionTypeMiddle;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(swichOrganAction:)]) {
        [self.delegate swichOrganAction:self.dataArray[indexPath.row]];
    }
    [self hiddeOrganViewAnimation];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(100)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGHomeOrganCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#F5F8FE"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 1)];
    }
    return _tableView;
}

-(UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _closeBtn;
}

@end


#pragma mark
#pragma mark ====> LGHomeOrganCell

@interface LGHomeOrganCell()
/**   */
@property (nonatomic , strong)UIImageView *iconView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIImageView *tipView;

@end

@implementation LGHomeOrganCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setType:(LGPositionType)type{
    [super setType:type];
    CGFloat iconTop = viewPix(12);
    CGFloat iconBottom = -viewPix(10);
    if (type == LGPositionTypeTop) {
        iconTop = viewPix(20);
        iconBottom = -viewPix(10);
        
    }else if (type == LGPositionTypeBottom){
        iconTop = viewPix(12);
        iconBottom = -viewPix(18);
        
    }else if (type == LGPositionTypeSingle){
        iconTop = viewPix(20);
        iconBottom = -viewPix(18);
    }
    [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(iconTop);
        make.bottom.equalTo(self.contentView).offset(iconBottom);
    }];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:LGNSString(dataDic[@"level_logo"])]];
    self.titleLabel.text = LGNSString(dataDic[@"level_name"]);
    self.tipView.hidden = [LGNSString(dataDic[@"status"]) isEqualToString:@"1"]?NO:YES;
}

-(void)createSubView{
    [self.contentView addSubview:self.iconView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.tipView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(12));
        make.left.equalTo(self.contentView).offset(viewPix(28));
        make.bottom.equalTo(self.contentView).offset(-viewPix(10));
        make.width.height.equalTo(@(viewPix(40)));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(viewPix(8));
        make.right.equalTo(self.tipView.mas_left).offset(-viewPix(10));
        make.centerY.equalTo(self.iconView);
    }];
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(28));
        make.centerY.equalTo(self.iconView);
        make.width.height.equalTo(@(viewPix(20)));
    }];
}

-(UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(28), viewPix(12), viewPix(40), viewPix(40))];
        _iconView.cornerRidus = 5;
    }
    return _iconView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _titleLabel;
}

-(UIImageView *)tipView{
    if(!_tipView){
        _tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"药球患者-完成"]];
    }
    return _tipView;
}



@end
